extension NumExtensions on num {
  /// 🕒 快速生成 Duration
  Duration get milliseconds => Duration(milliseconds: toInt());
  Duration get seconds => Duration(seconds: toInt());
  Duration get minutes => Duration(minutes: toInt());
  Duration get hours => Duration(hours: toInt());

  /// 💾 单位转换
  double get kb => this * 1024;
  double get mb => this * 1024 * 1024;

  /// 💰 格式化小数（默认保留2位）
  String toFixedString([int fractionDigits = 2]) =>
      toStringAsFixed(fractionDigits);

  /// 💲 格式化为金额（带千分位）
  String toPriceString({int fractionDigits = 2}) {
    final value = toStringAsFixed(fractionDigits);
    final parts = value.split('.');
    final intPart = parts[0];
    final buffer = StringBuffer();
    for (int i = 0; i < intPart.length; i++) {
      final reversedIndex = intPart.length - i - 1;
      buffer.write(intPart[reversedIndex]);
      if ((i + 1) % 3 == 0 && reversedIndex != 0) buffer.write(',');
    }
    final formattedInt = buffer.toString().split('').reversed.join('');
    return parts.length > 1 ? '$formattedInt.${parts[1]}' : formattedInt;
  }

  /// 🧮 安全除法（防止除零）
  double safeDivide(num divisor, {double fallback = 0.0}) {
    if (divisor == 0) return fallback;
    return this / divisor;
  }

  /// 📏 限制在指定范围内
  num clampBetween(num min, num max) =>
      this < min ? min : (this > max ? max : this);

  /// 🕰 从时间戳生成 DateTime
  DateTime get toDateTimeFromEpoch {
    // 自动判断是秒级还是毫秒级
    if (this > 10000000000) {
      return DateTime.fromMillisecondsSinceEpoch(toInt());
    } else {
      return DateTime.fromMillisecondsSinceEpoch(toInt() * 1000);
    }
  }

  /// 📱 屏幕尺寸辅助（示例：屏幕百分比）
  double percentOf(double total) => total * (this / 100);
}
